#!/usr/bin/python
# -*- coding: utf-8 -*-

# ESiPC
from esipc import ESiPCProperty

from mikan.view import *
from mikan.viewlet.jqmviewlet import *

# ------------------------------------------------------------
## MIKAN jQuery Mobile テンプレート XHTMLビュークラス
#
class MikanJQMTemplateXHTMLView(MikanTemplateXHTMLView):
	def set_title(self, title):
		self.title.title = title + " - " + self.request.config.get("site", "title")

	def get_body(self):
		return self.FBody

	def get_viewport(self):
		return self.FViewport

	def __init__(self, r, style=None):
		super(MikanJQMTemplateXHTMLView, self).__init__(r)

		self.FViewport = MikanMetaViewlet(self.head, "viewport", "width=device-width, initial-scale=1")
		self.FBody = MikanBodyViewlet(self.html)

	def perform(self):
		return super(MikanJQMTemplateXHTMLView, self).perform()

	# ----------------------------------------------------
	## Property
	body = ESiPCProperty(get_body, None, None)
	viewport = ESiPCProperty(get_viewport, None, None)

# ------------------------------------------------------------
## MIKAN jQuery Mobile マルチページ テンプレート XHTMLビュークラス
#
class MikanJQMMultiPagesTemplateXHTMLView(MikanJQMTemplateXHTMLView):

	def get_pages(self):
		return self.FPages

	def get_multi_pages(self):
		return self.FMultiPages

	def __init__(self, r, style=None):
		super(MikanJQMMultiPagesTemplateXHTMLView, self).__init__(r)

		self.FMultiPages = MikanJQMMultiPagesViewlet(self.body)

		self.FPages = {}

	def addPage(self, id):
		return None

	def perform(self):
		return super(MikanJQMMultiPagesTemplateXHTMLView, self).perform()

	# ----------------------------------------------------
	## Property
	pages = ESiPCProperty(get_pages, None, None)
	multipages = ESiPCProperty(get_multi_pages, None, None)

# ------------------------------------------------------------
## MIKAN jQuery Mobile マルチページ テンプレート XHTMLビュークラス
#
class MikanJQMDefaultTemplateXHTMLView(MikanJQMMultiPagesTemplateXHTMLView):

	def get_header_buttons(self):
		return self.FHeaderButtons

	def __init__(self, r, style=None):
		super(MikanJQMDefaultTemplateXHTMLView, self).__init__(r)

		self.FHeaderButtons = []

	def addPage(self, id, title):
		page = MikanJQMPageTabletViewlet(self.multipages, id, title)
		self.pages[id] = page

		MikanH5Viewlet(page.footer, self.request.config.get("site", "description"))
		MikanH5Viewlet(page.footer, self.request.config.get("site", "copyright"))

		return page

	def perform(self):
		if len(self.pages) > 1:
			for i in self.pages.itervalues():
				MikanJQMLiViewlet(i.ssubmenus, "list-divider", u"ページメニュー")

				count = 0
				for k,v in self.pages.iteritems():
					self.logger.debug(i)
					self.logger.debug(k)

					li = MikanLiViewlet(i.ssubmenus)
					li.sortkey = 1000 + count
					count = count + 1
					if (k == i.id):
						li.clazz = "current"
						li.nodevalue = v.title
					else:
						MikanJQMAViewlet(li, "#" + k, v.title)

		for p in self.pages.itervalues():
			for b in self.headerbuttons:
				MikanJQMIconViewlet(p.header.icons, b.href, b.text, b.css, b.external, b.iconpos, b.icon, b.transition)

		return super(MikanJQMDefaultTemplateXHTMLView, self).perform()

	# ----------------------------------------------------
	## Property
	headerbuttons = ESiPCProperty(get_header_buttons, None, None)
